/**数值格式化 numberFormat(<格式化的数值>, <保留的小数位>) */
export const numberFormat = (number: number | string, decimalDigits: number | string = 2) : number => {
  let formatted: number = Number(parseFloat(number as string).toFixed(decimalDigits as number).replace(/\.?0+$/, ""))
  formatted = isNaN(formatted) ? 0 : Number(formatted)
  return formatted;
}

/**对象或数组深拷贝 */
export const deepClone = (obj: ObjectAny | any[]): ObjectAny | any[] => {
  if (Array.isArray(obj)) {
    const newArr = [];
    for (let i = 0; i < obj.length; i++) {
      if (typeof obj[i] === 'object') {
        newArr.push(deepClone(obj[i]));
      } else {
        newArr.push(obj[i]);
      }
    }
    return newArr;
  } else if (typeof obj === 'object') {
    const newObj = {} as ObjectAny;
    for (const key in obj) {
      if (Object.prototype.hasOwnProperty.call(obj, key)) {
        if (typeof obj[key] === 'object') {
          newObj[key] = deepClone(obj[key]);
        } else {
          newObj[key] = obj[key];
        }
      }
    }
    return newObj;
  } else {
    return obj;
  }
}

/**触发失焦 */
export const autoBlur = () => {
  const autoFocusElement = document.createElement('input')
  document.body.appendChild(autoFocusElement)
  autoFocusElement.focus()
  autoFocusElement.remove()
}